package com.bootdo.system.controller;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.domain.DataBaseLogDO;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.domain.UserExDO;
import com.bootdo.system.service.UserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.AothorityDO;
import com.bootdo.system.service.AothorityService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021-01-11 14:06:08
 */
 
@Controller
@RequestMapping("/system/aothority")
public class AothorityController extends BaseController {

	@Autowired
	private AothorityService aothorityService;

	@Autowired
	private UserService userService;

	@Autowired
	private BootdoConfig bootdoConfig;
	
	@GetMapping("/list")
	String Aothority(Model model){
		model.addAttribute("userId",getUserId());
	    return "system/aothority/aothority";
	}

	@GetMapping("/checkList")
	String checkList(){
		return "system/aothority/aothorityCheck";
	}
	
	@ResponseBody
	@GetMapping("/listFor")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<AothorityDO> aothorityList = aothorityService.list(query);
		int total = aothorityService.count(query);
		PageUtils pageUtils = new PageUtils(aothorityList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "system/aothority/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		AothorityDO aothority = aothorityService.get(id);
		model.addAttribute("aothority", aothority);
	    return "system/aothority/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( AothorityDO aothority){
		aothority.setUserId(getUserId());
		aothority.setCreateDate(new Date());
		aothority.setStatus("1");//待审核

		Map<String,Object> map = new HashMap<String, Object>();
		map.put("userId",getUserId());
		//判断该用户是否重复申请
		AothorityDO aothorityDO1 = aothorityService.getAothorityDetail(map);
		if(null != aothorityDO1) {
			return R.error("该用户已经申请文件操作权限");
		}
		if(aothorityService.save(aothority)>0){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( AothorityDO aothority){
		aothority.setStatus("1");//待审核
		aothorityService.update(aothority);
		return R.ok();
	}

	/**
	 * 审核
	 */
	@ResponseBody
	@PostMapping("/check")
	public R check( AothorityDO aothority){
		if(StringUtils.equals("2",aothority.getStatus())) {
			//审核通过
			AothorityDO aothority1 = aothorityService.get(aothority.getId());

			DriverManagerDataSource dataSource=new DriverManagerDataSource();
			dataSource.setDriverClassName("com.mysql.jdbc.Driver");
			dataSource.setUrl("jdbc:mysql://" + bootdoConfig.getLocalDataBaseIp() + ":" + bootdoConfig.getRemoteDataBasePort() + "/" + bootdoConfig.getRemoteDataBaseName());
			dataSource.setUsername(bootdoConfig.getRemoteDataBaseUser());
			dataSource.setPassword(bootdoConfig.getRemoteDataBasePassword());
			JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

			StringBuffer buffer1 = new StringBuffer();
			buffer1.append("update baidujiex.upload_download_authority set upload_authority = " + aothority1.getUploadAuthority());
			buffer1.append(", download_authority = " + aothority1.getDownloadAuthority());

			StringBuffer buffer2 = new StringBuffer();
			buffer2.append("select user_id from baidujiex.upload_download_authority");
			//修改该当前用户
			buffer2.append(" where user_id = " + aothority1.getUserId());
			//查询当前用户是否存在，存在则修改，否则添加
			List<DataBaseLogDO> list = jdbcTemplate.query(buffer2.toString(), new RowMapper() {
				@Override
				public Object mapRow(ResultSet resultSet, int i) throws SQLException {
					UserExDO userExDO = new UserExDO();
					return userExDO;
				}
			});
			if(CollectionUtils.isEmpty(list)) {
				UserDO userDO1 = userService.get(aothority1.getUserId());
				String sql = "insert into baidujiex.upload_download_authority(user_id,upload_authority,download_authority,dept_id) values (" + aothority1.getUserId() +"," + aothority1.getUploadAuthority()+","+aothority1.getDownloadAuthority() + ","+userDO1.getDeptId()+")";
				jdbcTemplate.execute(sql);
			}else {
				buffer1.append(" where user_id = " + aothority1.getUserId());
				jdbcTemplate.execute(buffer1.toString());
			}
		}
		if(aothorityService.update(aothority)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long id){
		if(aothorityService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids){
		aothorityService.batchRemove(ids);
		return R.ok();
	}
	
}
